﻿namespace Ki.GoogleEarth
open System.Xml;
open System.Xml.Linq;
module KMLParts =

    let Namespace = XNamespace.Get("http://www.opengis.net/kml/2.2")

    let private LatitudeSectionSize tSections =
        180.0m / tSections
    let private LongitudeSectionSize tSections =
        360.0m / tSections

    let private SouthLatitude (tY:int) tSections =
        -90.0m + LatitudeSectionSize(tSections) * (decimal)tY
    let private NorthLatitude (tY:int) tSections =
        -90.0m + LatitudeSectionSize(tSections) * ((decimal)tY+1.0m)

    let private EastLongitude (tX:int) tSections =
        180.0m - LongitudeSectionSize(tSections) * (decimal)tX
    let private WestLongitude (tX:int) tSections =
        180.0m - LongitudeSectionSize(tSections) * ((decimal)tX+1.0m)
    let private MinLOD (tResult:XElement) tMinLOD =
        match tMinLOD with
        | 0 -> ()
        | _ -> tResult.Add(new XElement(Namespace + "minLodPixels", tMinLOD))
    let private MaxLOD (tResult:XElement) tMaxLOD =
        match tMaxLOD with
        | 0 -> ()
        | _ -> tResult.Add(new XElement(Namespace + "maxLodPixels", tMaxLOD))
    let private LOD tMinLOD tMaxLOD =
        let vResult = new XElement(Namespace + "Lod")
        MinLOD vResult tMinLOD
        MaxLOD vResult tMaxLOD
        vResult

    let private GroundOverlay (tName:string) tLayer X Y tSections =
        new XElement(Namespace + "GroundOverlay",
            new XElement(Namespace + "name", tName),
            new XElement(Namespace + "Icon",
                new XElement(Namespace + "viewBoundScale", "0.75"),
                new XElement(Namespace + "href", (Ki.FractalTerrains.Utilities.FileName tLayer X Y) + ".jpg")
            ),
            new XElement(Namespace + "LatLonBox",
                new XElement(Namespace + "north", (NorthLatitude Y tSections)),
                new XElement(Namespace + "south", (SouthLatitude Y tSections)),
                new XElement(Namespace + "east", (EastLongitude X tSections)),
                new XElement(Namespace + "west", (WestLongitude X tSections))
            )
        )

    let private Region X Y tSections tMaxLOD tMinLOD =
        new XElement(Namespace + "Region",
            new XElement(Namespace + "LatLonAltBox",
                new XElement(Namespace + "north", (NorthLatitude Y tSections)),
                new XElement(Namespace + "south", (SouthLatitude Y tSections)),
                new XElement(Namespace + "east", (EastLongitude X tSections)),
                new XElement(Namespace + "west", (WestLongitude X tSections))),
            (LOD tMinLOD tMaxLOD))

    let private NetworkLink (tName:string) (tLayer:int) X Y tSections tMaxLOD tMinLOD =
        new XElement(Namespace + "NetworkLink",
            new XElement(Namespace + "name", tName),
            (Region X Y tSections tMaxLOD tMinLOD),
            new XElement(Namespace + "Link",
                new XElement(Namespace + "href",  "..\\Layer" + tLayer.ToString() + "\\" + (Ki.FractalTerrains.Utilities.FileName tLayer X Y) + ".kml"),
                new XElement(Namespace + "viewRefreshMode", "onRegion")))
        
    let LayerFile (tName:string) tLayer X Y tSections tMaxLOD tMinLOD =
        let vTemp = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"))
        vTemp.Add(
            new XElement(Namespace + "kml",
                new XElement(Namespace + "Document",
                    (Region X Y tSections tMaxLOD tMinLOD),
                    (GroundOverlay tName tLayer X Y tSections),
                    (NetworkLink tName (tLayer+1) (X*2+0) (Y*2+0) (tSections*2m) tMaxLOD tMinLOD),
                    (NetworkLink tName (tLayer+1) (X*2+1) (Y*2+0) (tSections*2m) tMaxLOD tMinLOD),
                    (NetworkLink tName (tLayer+1) (X*2+0) (Y*2+1) (tSections*2m) tMaxLOD tMinLOD),
                    (NetworkLink tName (tLayer+1) (X*2+1) (Y*2+1) (tSections*2m) tMaxLOD tMinLOD)
                )
            )
        )
        vTemp
    let SuperOverlay =
        let vTemp = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"))
        vTemp.Add(
            XElement
                (   Namespace + "kml",
                    new XElement(Namespace + "NetworkLink",
                        new XElement(Namespace + "name", "Super Overlay"),
                        (Region 0 0 1m -1 0),
                        new XElement(Namespace + "Link",
                            new XElement(Namespace + "href", "Layer0\\" + (Ki.FractalTerrains.Utilities.FileName 0 0 0) + ".kml"),
                            new XElement(Namespace + "viewRefreshMode", "onRegion")))
                )
            )
        vTemp